//
//  HNSH_OpenContent.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/8/12.
//

#import "HNSH_OpenContent.h"

@implementation HNSH_OpenContent

+ (instancetype)objectWithCount:(NSString *)count price:(NSString *)price des:(NSString *)des {
    HNSH_OpenContent * emoj = [[HNSH_OpenContent alloc] init];
    if (emoj) {
        emoj.count = count;
        emoj.price = price;
        emoj.des = des;
    }
    return emoj;
}

- (NSData *)encode{
    NSMutableDictionary *dataDict = [NSMutableDictionary dictionary];
    [dataDict setObject:self.count forKey:@"count"];
    [dataDict setObject:self.price forKey:@"price"];
    [dataDict setObject:self.des forKey:@"des"];
    [dataDict setObject:self.recordId forKey:@"id"];
    [dataDict setObject:@(self.status) forKey:@"status"];
    
    [dataDict setObject:self.openUsername forKey:@"openUsername"];
    [dataDict setObject:self.openUserId forKey:@"openUserId"];
    [dataDict setObject:self.packetUserId forKey:@"packetUserId"];
    [dataDict setObject:self.packetUsername forKey:@"packetUsername"];

    if (self.senderUserInfo) {
        NSMutableDictionary *userInfoDic = [[NSMutableDictionary alloc] init];
        if (self.senderUserInfo.name) {
            [userInfoDic setObject:self.senderUserInfo.name forKeyedSubscript:@"name"];
        }
        if (self.senderUserInfo.portraitUri) {
            [userInfoDic setObject:self.senderUserInfo.portraitUri forKeyedSubscript:@"portrait"];
        }
        if (self.senderUserInfo.userId) {
            [userInfoDic setObject:self.senderUserInfo.userId forKeyedSubscript:@"id"];
        }
        if (self.senderUserInfo.extra) {
            [userInfoDic setObject:self.senderUserInfo.extra forKeyedSubscript:@"extra"];
        }
        [dataDict setObject:userInfoDic forKey:@"user"];
    }
    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dataDict options:NSJSONWritingPrettyPrinted error:nil];
    return jsonData;
}

- (void)decodeWithData:(NSData *)data{
    if (data) {
        __autoreleasing NSError *error = nil;
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
        if (dic) {
            self.count = [NSString stringWithFormat:@"%@", dic[@"count"]];
            self.price = [NSString stringWithFormat:@"%@", dic[@"price"]];
            self.des = [NSString stringWithFormat:@"%@", dic[@"des"]];
            self.recordId = [NSString stringWithFormat:@"%@", dic[@"id"]];
            self.status = [dic[@"status"] integerValue];
            
            self.openUsername = [NSString stringWithFormat:@"%@", dic[@"openUsername"]];
            self.openUserId = [NSString stringWithFormat:@"%@", dic[@"openUserId"]];
            self.packetUserId = [NSString stringWithFormat:@"%@", dic[@"packetUserId"]];
            self.packetUsername = [NSString stringWithFormat:@"%@", dic[@"packetUsername"]];
            NSDictionary * userinfoDic = dic[@"user"];
            if (userinfoDic != nil) {
                [self decodeUserInfo:userinfoDic];
            }
        }
    }
}

+ (NSString *)getObjectName{
    return @"app:red_packet_open";
}

+ (RCMessagePersistent)persistentFlag{
    return MessagePersistent_ISCOUNTED;
}

- (NSString *)conversationDigest{
    return @"[打开购物值红包]";
}

@end
